package test.luin.pers.myapplication;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import test.luin.pers.myapplication.common.DialogMsg;

import static android.content.ContentValues.TAG;

public class BasicReceive extends BroadcastReceiver implements constant {
    boolean isSave = false;
    boolean isSaveAs = false;
    private String ServerUrl = "http://192.168.2.174:5566/iWebOfficeService.data?action=ProcessRequest";
    private String uploadFilePath = SDCARD_ROOT_PATH + "/ZHT/doc/test2.doc";
    private String  returnResult;
    @Override
    public void onReceive(Context context, Intent intent) {
        if (BROADCAST_BACK_DOWN.equals(intent.getAction())) {
            Log.d(TAG, "key back down");
        } else if (BROADCAST_HOME_DOWN.equals(intent.getAction())) {
            Log.d(TAG, "key home down");
        } else if (BROADCAST_FILE_SAVE.equals(intent.getAction())) {
            isSave = true;
            Log.d(TAG, BROADCAST_FILE_SAVE + " : file save");
        } else if (BROADCAST_FILE_CLOSE.equals(intent.getAction())) {
            fileClose((Basic) context);
            isSave = false;
            Log.d(TAG, BROADCAST_FILE_CLOSE + " : file close");
        } else if ("com.kinggrid.notfind.office".equals(intent.getAction())) {
            Log.d(TAG, "wps office not find");
        } else if (BROADCAST_FILE_SAVE_PIC.equals(intent.getAction())) {
            Log.d(TAG, "office save pic over");
        } else if (BROADCAST_FILE_SAVEAS_PDF.equals(intent.getAction())) {
            Log.d(TAG, "office save as pdf over");
        } else if (BROADCAST_FILE_SAVEAS.equals(intent.getAction())) {
            isSaveAs = true;
        }
    }

    public void fileClose(Basic basic) {
        // 工厂设计模式，得到创建对话框的工厂
        AlertDialog.Builder builder = new AlertDialog.Builder(basic);
        builder.setTitle("提示");
        builder.setMessage("是否保存到服务器？");
        builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            Thread thread = null;
            @Override
            public void onClick(DialogInterface dialog, int which) {
                thread = new ThreadWatchar();
                thread.start();
                try {
                    thread.join();
                    if(returnResult.length()>0)
                    {
                        Toast.makeText(basic, "上传失败！"+returnResult, Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(basic, "上传成功！"+returnResult, Toast.LENGTH_LONG).show();
                    }
                } catch (InterruptedException e) {
                    Toast.makeText(basic, "上传失败！"+returnResult+e, Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }
        });
        builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(basic, "选择否", Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }

    class ThreadWatchar extends Thread{
        @Override
        public void run(){
            returnResult = uploadFile();
        }
    }
    //将文件上传到服务器
    public String uploadFile() {
        //上传的文件路径
        Gson gson = new Gson();
        Map<String, String> map = new HashMap<String, String>();
        //服务器路径
        map.put("FILEPATH", "D:\\test2.doc");
        map.put("OPTION", "SAVEFILE");
        String formData = null;
        try {
            formData = URLEncoder.encode(gson.toJson(map), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String urlParam = ServerUrl + "&FormData=" + formData;

       String result = IWebOfficeHandler.uploadFile(urlParam, new String[]{uploadFilePath});
       return result;
    }
}
